﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Coding4Fun.Phone.Controls;
using Microsoft.Phone.Controls;

namespace DSA
{
    public partial class ListEntryItem : UserControl
    {

        public string ImageSource = "";

        public ListEntryItem()
        {
            InitializeComponent();
        }

        private void chkItemName_Checked(object sender, RoutedEventArgs e)
        {
            txtPrice.Foreground = new SolidColorBrush(Colors.Gray);

            if (txtPrice.Text == "$0.00")
                CheckedClick(null, null);
        }

        private void chkItemName_Unchecked(object sender, RoutedEventArgs e)
        {
            txtPrice.Foreground = new SolidColorBrush(Colors.Red);
        }

        private void EditNameClick(object sender, EventArgs e)
        {
            InputPrompt input = new InputPrompt();
            input.Completed += Rename_Completed;
            input.Title = "Edit Item Name";
            input.Message = "New name for item";
            input.Show();
        }

        private void Rename_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Ok && !String.IsNullOrEmpty(e.Result))
            {
                this.txtName.Text = e.Result;
            }
        }

        private void EditQuantityClick(object sender, RoutedEventArgs e)
        {
            
        }

        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
            PivotPage1 pg = frame.Content as PivotPage1;

            pg.listboxItemList.Items.Remove(this);
        }

        private void CheckedClick(object sender, EventArgs e)
        {
            InputPrompt input = new InputPrompt();
            input.Completed += Checked_Completed;
            input.Title = "Please enter Price";
            input.Message = "Price of item per unit";
            input.Show();
        }

        private void Checked_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Ok && !String.IsNullOrEmpty(e.Result))
            {
                float temp = 0;
                if (float.TryParse(e.Result, out temp))
                {
                    this.txtPrice.Text = "$" + temp;

                    PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                    PivotPage1 pg = frame.Content as PivotPage1;
                    pg.listboxItemList_Tap(null, null);
                }
                else
                {
                    this.chkItemName.IsChecked = false;
                }
            }
        }
    }
}
